﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using FinalProject.DAL.Model;
using FinalProject.DAL.Mapping;

namespace FinalProject.BL
{
    public class OrderManagementBL : BaseBL<OrderDocument>
    {
        public enum PaymentMethod : short { Cash = 0, Bank = 1 }

        public static Guid GetOrderJournalId()
        {
            Guid result = Guid.Empty;
            string orderJournal = "ORDER";
            result = dalContext.Journals.Where(p => p.Code == orderJournal)
                                            .Select(p => p.Id)
                                            .SingleOrDefault();
            return result;
        }

        public static IDictionary<Guid, string> GetValueForItemCombobox()
        {
            IDictionary<Guid, string> result = new Dictionary<Guid, string>();
            try
            {
                result = dalContext.Items.ToDictionary(p => p.Id, g => g.Name);
                return result;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static long GetLastestNumber()
        {
            try
            {
                long result = 0;
                result = dalContext.OrderDocuments
                                    .OrderByDescending(p => p.DocNumber)
                                    .Select(p => p.DocNumber)
                                    .FirstOrDefault();
                if (result == 0)
                    result = 1;

                return result + 1;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static bool InsertOrUpdate(OrderDocument orderDoc, IList<DocumentRow> lstItem)
        {
            try
            {
                if (orderDoc.Id == Guid.Empty)
                    dalContext.OrderDocuments.AddObject(orderDoc);
                foreach (var item in lstItem)
                {
                    if (item.Id == Guid.Empty)
                    {
                        DocumentRow newRow = new DocumentRow()
                        {
                            ItemId = item.ItemId,
                            RowNumber = lstItem.IndexOf(item),
                            UnitPrice = item.UnitPrice,
                            Quantity = item.Quantity,
                            TotalAmount = item.TotalAmount,
                            Description = item.Description,
                            DeliveryDocumentId = null,
                            InvoiceDocumentId = null
                        };
                        orderDoc.DocumentRows.Add(newRow);
                    }
                }
                dalContext.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static IList<DocumentRow> LoadAllRow(Guid docId)
        {
            try
            {
                IList<DocumentRow> result = dalContext.DocumentRows.Where(p => p.OrderDocumentId == docId).ToList();
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static IDictionary<Guid, long> GetAllOrderDocumentItem()
        {
            try
            {
                var query = dalContext.OrderDocuments.Select(p => new { Id = p.Id, Number = p.DocNumber });
                if (query.Count() <= 0)
                    return null;

                return query.ToDictionary(p => p.Id, g => g.Number);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }

    //public class OrderDocumentRowItem
    //{
    //    public Guid RowId { get; set; }
    //    public Guid ItemId { get; set; }
    //    public string ItemCode { get; set; }
    //    public string Description { get; set; }
    //    public Guid WarehouseId { get; set; }
    //    public string WarehouseCode { get; set; }
    //    public int Quantity { get; set; }
    //    public decimal Price { get; set; }
    //    public decimal TotalAmount { get; set; }
    //}

}
